package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.CashWithdrawals;
import com.hyk.dto.R;
import com.hyk.service.CashWithdrawalsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@Api("GCN提现控制器")
@RequestMapping("/cashWithdrawals")
public class CashWithdrawalsController {

    @Resource
    private CashWithdrawalsService cashWithdrawalsService;

    @PostMapping("/sell")
    public R sell(@RequestBody CashWithdrawals cashWithdrawals){
        cashWithdrawalsService.sell(cashWithdrawals);
        return R.ok();
    }

    @GetMapping("/user/records")
    public R<Page<CashWithdrawals>> listForUser(Page<CashWithdrawals> page, Integer status){
        QueryWrapper<CashWithdrawals> queryWrapper = new QueryWrapper<>();
        Long userId = Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq(status != null,"status",status);
        queryWrapper.orderByDesc("created");
        Page<CashWithdrawals> page1 = cashWithdrawalsService.page(page, queryWrapper);
        return R.ok(page1);
    }

    @GetMapping("/records")
    @ApiOperation("按条件查询")
    public R<Page<CashWithdrawals>> list(@ApiIgnore Page<CashWithdrawals> page,
                                         Long userId,
                                         String userName,
                                         String mobile,
                                         Byte status,
                                         String numMin,
                                         String numMax,
                                         String startTime,
                                         String endTime){
        Page<CashWithdrawals> page1 = cashWithdrawalsService.findByPage(page,userId,userName,mobile,status,numMin,numMax,startTime,endTime);
        return R.ok(page1);
    }

    @PostMapping("/updateWithdrawalsStatus")
    @ApiOperation("提现审核")
    public R updateWithdrawalsStatus(@RequestBody CashWithdrawals cashWithdrawals){
        cashWithdrawalsService.changeStatus(cashWithdrawals);
        return R.ok();
    }

}
